
var page_result = 5;
$.urlParam = function(name, url) {
    if (!url) {
        url = window.location.href;
    }
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url);
    if (!results) {
        return 0;
    }
    return results[1] || 0;
};

function change_alias(alias)
{
    var str = alias;
    str = str.toLowerCase();
    str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
    str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
    str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
    str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
    str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
    str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
    str = str.replace(/đ/g, "d");
    //str= str.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,"-");
    /* tìm và thay thế các kí tự đặc biệt trong chuỗi sang kí tự - */
    str = str.replace(/-+-/g, "-"); //thay thế 2- thành 1-
    str = str.replace(/^\-+|\-+$/g, "");
    //cắt bỏ ký tự - ở đầu và cuối chuỗi 
    return str;
}

function float2int(value) {
    return ~~value;
}

function pager(numberResutl) {
    var href = window.location.origin + window.location.pathname;
    var key = $.urlParam('key');
    var curent_page = ($.urlParam('page') === 0) ? 1 : $.urlParam('page');
    curent_page = parseInt(curent_page);
    var text = "";
    var numberpage = Math.ceil((numberResutl / page_result));
    //co 1 trang
    if (numberResutl <= page_result) {
        return;
    } else if (numberResutl <= (page_result * 5)) {
        // nho hon =5 trang
        for (var i = 1; i <= numberpage; i++) {
            if (curent_page === i) {
                text += "<a class='number_element active_page' href='" + href + i + "'>" + i + "</a>";
            } else {
                text += "<a class='number_element' href='" + href + i + "'>" + i + "</a>";
            }
        }
    } else {
        // nhieu hon 5 trang
        href = href + "?key=" + key + "&page=";
        console.log(href);
        var start_page = curent_page - 2;
        var end_page = curent_page + 2;
        if (start_page <= 1) {
            end_page = end_page + 1 - (start_page);
            start_page = 1;
        }
        if (curent_page !== start_page) {
            text += "<a class='number_element'  href='" + href + 1 + "''>&laquo;</a>";
            text += "<a class='number_element'  href='" + href + (curent_page - 1) + "''>&lsaquo;</a>";
        }
        if (curent_page === numberpage) {
            end_page = curent_page;
            start_page = curent_page - 4;
        }

        if (end_page > numberpage) {
            end_page = numberpage;
            start_page = curent_page - 2 - (numberpage - curent_page);
        }

        for (var i = start_page; i <= end_page; i++) {
            if (curent_page === i) {
                text += "<a class='number_element active_page' href='" + href + i + "'>" + i + "</a>";
            } else {
                text += "<a class='number_element' href='" + href + i + "'>" + i + "</a>";
            }
        }
        if (curent_page !== numberpage) {
            text += "<a class='number_element'  href='" + href + (curent_page + 1) + "''>&rsaquo;</a>";
            text += "<a class='number_element'  href='" + href + numberpage + "''>&raquo;</a>";
        }
    }
    $('<div class="page_number"></div>').appendTo($('#post_index_content'));
    $('div.page_number').html(text);
    $('a.active_page').click(function(e) {
        return false;
    });
}


function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}


function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) === 0)
            return c.substring(name.length, c.length);
    }
    return "";
}

function delete_cookie( name ) {
  document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}